/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.enums.PaymentType;
import br.com.foxline.enums.Status;
import br.com.foxline.enums.StatusSale;
import br.com.foxline.enums.TypeAddress;
import br.com.foxline.model.Customer;
import br.com.foxline.model.Pending;
import br.com.foxline.model.Sale;
import br.com.foxline.model.Users;
import br.com.foxline.service.CustomerService;
import br.com.foxline.service.GroupService;
import br.com.foxline.service.LotService;
import br.com.foxline.service.PendingService;
import br.com.foxline.service.SaleService;
import br.com.foxline.service.UserService;
import br.com.foxline.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerApproveSale implements Serializable {

    private Sale sale;
    private Customer customer;
    @EJB
    private SaleService saleService;
    @EJB
    private CustomerService customerService;
    @EJB
    private PendingService pendingService;
    @EJB
    private UserService userService;
    @EJB
    private GroupService groupService;
    @EJB
    private LotService lotService;
    private Pending pending;
    private boolean canApprove = false;

    @PostConstruct
    public void init() {
        this.pending = new Pending();
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String saleId = params.get("saleId");
        this.sale = saleService.find(Long.valueOf(saleId));
        canApprove = saleService.verifyIfCanApprove(this.sale);
        this.customer = customerService.find(this.sale.getCustomer().getId());
    }

    public void approve() {
        Pending p = new Pending();
        p.setSale(sale);
        p.setDataPendencia(new Date());
        p.setDescription("Venda Aprovada");
        p.setStatusSale(StatusSale.FINALIZED);
        this.sale.setStatusSale(StatusSale.FINALIZED);
        this.sale.getLot().setStatus(Status.VENDIDO);
        try {
            this.saleService.update(sale);
            this.lotService.update(this.sale.getLot());
            p.setUsers(userService.getCurrentUserFull());
            pendingService.save(p);
            Msg.messagemInfo("Venda Aprovada");
        } catch (Exception e) {
            Msg.messagemError("Erro ao aprovar a venda");
        }
    }

    public void reprove() {
        this.pending.setDataPendencia(new Date());
        this.pending.setSale(sale);
        this.sale.setStatusSale(pending.getStatusSale());
        try {
            saleService.update(sale);
            pendingService.save(pending);
            Msg.messagemInfo("Venda Reprovada.");
        } catch (Exception e) {
            Msg.messagemError("Erro ao reprovar a venda");
        }
    }

    public List<SelectItem> pendingType() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (StatusSale c : StatusSale.values()) {
            items.add(new SelectItem(c, c.getName()));
        }
        return items;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Sale getSale() {
        return sale;
    }

    public void setSale(Sale sale) {
        this.sale = sale;
    }

    public Pending getPending() {
        return pending;
    }

    public void setPending(Pending pending) {
        this.pending = pending;
    }

    public boolean isCanApprove() {
        return canApprove;
    }

    public void setCanApprove(boolean canApprove) {
        this.canApprove = canApprove;
    }

    public void reportContract(Long l) {
        System.err.println("CONTRATO");
        sale = saleService.find(l);
        saleService.reportsContract(sale);
    }

    public void reportsProposedContract(Long l) {
        sale = saleService.find(l);
        saleService.reportsProposedContract(sale);
    }

    public boolean canEdit(Sale s) {
        if (s.getStatusSale().equals(StatusSale.FINALIZED)) {
            return false;
        }
        return true;
    }

    public boolean canGenerateContract(Sale s) {
        if (s.getStatusSale().equals(StatusSale.FINALIZED)) {
            return true;
        }
        return false;
    }
    
    public String dimensaoDoLote(){
        if(this.sale.getLot() == null || this.sale.getLot().getId() == null){
            return "";
        }
        if(this.sale.getLot().getDimessionX() != null && this.sale.getLot().getDimessionY()!= null){
            return this.sale.getLot().getDimessionX() + " X " + this.sale.getLot().getDimessionY();
        }else{
            return "VARIÁVEL";
        }
    }
    
}
